#!perl.exe

#*******************************************************}
#                                                       }
#       Yalgol/02 Compiler, Version 0.02 beta           }
#       Written by Y [17-10-00] <yallie@mail.ru>        }
#                                                       }
#       Perl script to generate cstrings.y02            }
#                                                       }
#       Copyright (c) 1999-2003 YALLIE, Inc.            }
#       All Rights Reserved.                            }
#                                                       }
#*******************************************************}

# $Id: cstrings.pl,v 1.4 2003/06/27 20:35:07 Y Exp $

$InputFile1 = "keywords";
$InputFile2 = "messages";
$InputFile3 = "symbols";
$OutputFile = "cstrings.y02";

open(InFile, $InputFile1) || die("Can't open file \"$InputFile1\""); 
@Words = <InFile>;
close(InFile);

open(OutFile, ">$OutputFile") || die("Can't create file \"$OutputFile\"");

# output source header
print OutFile "
/*******************************************************}
{                                                       }
{       Yalgol/02 Compiler, Version 0.02 beta           }
{       Generated by cstrings.pl, do not modify         }
{                                                       }
{       String constants                                }
{                                                       }
{       Copyright (c) 1999-2003 YALLIE, Inc.            }
{       All Rights Reserved.                            }
{                                                       }
{*******************************************************/

// \$Id\$

import
  Classes

// init Yalgol/02 language reserved words
class TYalgolReservedWords super TStrVector = public 
  create = (\n";

$CurrentLine = "    str Temp[] = (\"\"";
$MaxStrLength = 50;
$CurrentWord = 1;

# output keywords
foreach $Word (@Words) {
  $Word =~ s/\/\/.*$//;
  $Word =~ s/\s//g;

  if ($Word ne "") {
    $CurrentLine .= ",";
    $CurrentWord++;
    if (length($CurrentLine) > $MaxStrLength) {
      print OutFile "$CurrentLine\n";
      $CurrentLine = "     ";
    }
    $CurrentLine .= " \"$Word\""
  }
}
print OutFile "$CurrentLine)
    fCaseSensitive = True
    fItems = Temp
    fCount = fSize = $CurrentWord // # of keywords
  )

// reserved words constants
const
  rwUnknown = 0
";

$Count = 1;

# output keywords constants
foreach $Word (@Words) {
  $Word =~ s/^(.)/\U$1/;

  if ($Word ne "") {
    print (OutFile "  rw$Word = $Count\n");
    $LastWord = $Count;
    $Count++;
  }
}

print OutFile "  rwLastWord = $LastWord\n";

open(InFile, $InputFile2) || die("Can't open file \"$InputFile2\"");
@Messages = <InFile>;
close(InFile);

print OutFile "
// init compiler message list
class TYalgolMessages super TStrVector = public 
  create = (\n";

$CurrentLine = "    str Temp[] = (\"\"";
$CurrentWord = 1;

# create messages list
foreach $Message(@Messages) {
  $Msg = $Message;
  $Msg =~ s/\/\/.*$//;
  $Msg =~ s/\s+$//g;
  $Msg =~ s/"/\\"/g;
  if ($Msg ne "") {
    $CurrentWord++;
    $CurrentLine .= ",\n";
    print OutFile $CurrentLine;
    $CurrentLine = "      \"$Msg\"";
  }
}

print OutFile "$CurrentLine)
    fItems = Temp
    fCount = fSize = $CurrentWord // # of messages
  )

// compiler messages
const
";

$Count = 1;

# output constants
foreach $Message (@Messages) {
  $Message =~ s/\/\/.*$//;

  $Message =~ s/\"\;\"/semicolon/g;
  $Message =~ s/\"\,\"/comma/g;
  $Message =~ s/\"\.\"/dot/g;
  $Message =~ s/\"\=\"/assign/g;
  $Message =~ s/\"\*\"/asterisk/g;
  $Message =~ s/\"\)\"/right parenthesis/g;
  $Message =~ s/\"\(\"/left parenthesis/g;
  $Message =~ s/\"\]\"/right bracket/g;
  $Message =~ s/\"\[\"/left bracket/g;
  $Message =~ s/\"\:\"/colon/g;
  $Message =~ s/[\"\'\:]/ /g;
  $Message =~ s/\(.*\)//g;

  $Message =~ s/((\s+)|^)(.)/\u$3/g;
  $Message =~ s/\s//g;

  if ($Message ne "") {
    print (OutFile "  ce$Message = $Count\n");
    $Count++;
  }
}

# language symbols
open(InFile, $InputFile3) || die("Can't open file \"$InputFile3\"");
@Symbols = <InFile>;
close(InFile);

print OutFile "
// init Yalgol/02 symbols: operations and delimiters
class TYalgolSymbols super TStrVector = public 
  create = (\n";

$CurrentLine = "    str Temp[] = (\"\"";
$CurrentWord = 1;

# create symbols list
foreach $Symbol (@Symbols) {
  $Sym = $Symbol;
  $Sym =~ s/\/\/.*$//;
  $Sym =~ s/\s+$//g;
  $Sym =~ s/"/\\"/g;
  $Sym =~ s/\\/\\\\/g;
  if ($Sym ne "") {
    $CurrentLine .= ",";
    $CurrentWord++;
    if (length($CurrentLine) > $MaxStrLength) {
      print OutFile "$CurrentLine\n";
      $CurrentLine = "     ";
    }
    $CurrentLine .= " \"$Sym\""
  }
}

print OutFile "$CurrentLine)
    fItems = Temp
    fCount = fSize = $CurrentWord // # of symbols
  )

// Yalgol/02 symbols
const
  smUnknown = 0
";

$Count = 1;

# output constants
foreach $Symbol (@Symbols) {
  $Symbol =~ s/\/\/.*$//;

  $Symbol =~ s/\<\</ shl/g;
  $Symbol =~ s/\>\>/ shr/g;
  $Symbol =~ s/\&\&/ l and/g;
  $Symbol =~ s/\|\|/ l or/g;
  $Symbol =~ s/\+\+/ increase/g;
  $Symbol =~ s/\-\-/ decrease/g;
  $Symbol =~ s/\[\]/ brackets/g;
  $Symbol =~ s/\.\./ double dot/g;

  $Symbol =~ s/\>\=/ greater equal/g;
  $Symbol =~ s/\<\=/ less equal/g;
  $Symbol =~ s/\!\=/ not equal/g;
  $Symbol =~ s/\=\=/ double equal/g;
  $Symbol =~ s/\>/ greater/g;
  $Symbol =~ s/\</ less/g;

  $Symbol =~ s/\&/ and/g;
  $Symbol =~ s/\|/ or/g;
  $Symbol =~ s/\+/ plus/g;
  $Symbol =~ s/\-/ minus/g;
  $Symbol =~ s/\// div/g;
  $Symbol =~ s/\%/ mod/g;
  $Symbol =~ s/\^/ xor/g;
  $Symbol =~ s/\!/ not/g;
  $Symbol =~ s/\~/ tilde/g;
  $Symbol =~ s/\'/ apostrophe/g;
  $Symbol =~ s/\"/ quote/g;
  $Symbol =~ s/\;/ semicolon/g;
  $Symbol =~ s/\,/ comma/g;
  $Symbol =~ s/\./ dot/g;
  $Symbol =~ s/\=/ assign/g;
  $Symbol =~ s/\*/ mul/g;
  $Symbol =~ s/\)/ right parenthesis/g;
  $Symbol =~ s/\(/ left parenthesis/g;
  $Symbol =~ s/\]/ right bracket/g;
  $Symbol =~ s/\[/ left bracket/g;
  $Symbol =~ s/\@/ at/g;
  $Symbol =~ s/\:/ colon/g;
  $Symbol =~ s/\#/ sharp/g;
  $Symbol =~ s/\?/ question/g;
  $Symbol =~ s/\\/ back slash/g;
  $Symbol =~ s/\`/ back quote/g;
  $Symbol =~ s// /g;

  $Symbol =~ s/((\s+)|^)(.)/\u$3/g;
  $Symbol =~ s/\s//g;

  if ($Symbol ne "") {
    print (OutFile "  sm$Symbol = $Count\n");
    $LastSymbol = $Count;
    $Count++;
  }
}

print OutFile "  smLast = $LastSymbol

/*
  \$Log\$
  Revision 0.3  2003/06/26 21:42:46  Y
    * replaced TList instances with TStrVector
    + used static constant arrays (much less code generated)

  Revision 0.2  2002/08/21 23:16:34  Y
    * Yalgol/02 port

  Revision 0.1  2002/04/08 07:17:24  Y
    * adapted for Yalgol/02, version 0.01 alpha (Win32)

  Revision 0.0  2000/12/01 01:10:02  Y
    + just got started
    * see cstrings.pl for more log items
*/
";
close(OutFile);

# $Log: cstrings.pl,v $
# Revision 1.4  2003/06/27 20:35:07  Y
#   * renamed rwLast constant to rwLastWord (caused by 147.01 bug fix)
#
# Revision 1.3  2003/06/26 18:29:01  Y
#   * many changes in target code generation
#   * replaced TList instances with TStrVector
#   + used static constant arrays (much less code generated)
#
# Revision 1.2  2003/01/28 14:29:03  Y
#   * updated copyright
#
# Revision 1.1  2002/09/12 21:36:15  Y
#   * imported into CVS
#
# Revision 0.8  2002/08/23 00:17:21  Y
#   * adapted for Yalgol/02, version 0.02 beta
#
# Revision 0.7  2002/08/21 16:21:23  Y
#   + added colon handling for messages
#
# Revision 0.6  2002/08/11 18:08:01  Y
#   + added apostrophe handling for messages
#
# Revision 0.5  2002/04/07 23:33:25  Y
#   * adapted for Yalgol/02, version 0.01 alpha (Win32)
#
# Revision 0.4:  2000/12/04 20:13:53  Y
#   + added "[]", "@", ":", "#", "?", "\", "`"
#
# Revision 0.3:  2000/12/03 14:17:06  Y
#   + added rwLast and smLast constants
#
# Revision 0.2  2000/12/02 17:16:54  Y
#   + added symbols
#
# Revision 0.1  2000/12/02 01:04:41  Y
#   + initial implementation
#
# Revision 0.0  2000/11/30 23:17:20  Y
#   + just got started
